home *** CD-ROM | disk | FTP | other *** search
- #include "surround.h"
- #include <exec/types.h>
- #include <intuition/intuition.h>
- #include <intuition/IntuitionBase.h>
- #include <intuition/classusr.h>
- #include <intuition/pointerclass.h>
- #include <intuition/gadgetclass.h>
- #include <intuition/sghooks.h>
- #include <intuition/icclass.h>
- #include <graphics/displayinfo.h>
- #include <graphics/text.h>
- #include <graphics/gfx.h>
- #include <libraries/dos.h>
- #include <libraries/gadtools.h>
- #include <libraries/diskfont.h>
- #include <libraries/asl.h>
- #include <clib/exec_protos.h>
- #include <clib/intuition_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/graphics_protos.h>
- #include <clib/gadtools_protos.h>
- #include <clib/asl_protos.h>
- #include <clib/datatypes_protos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dos/dos.h>
- #include <dos/dosasl.h>
- #include <exec/exec.h>
- #include <proto/exec.h>
- #include <proto/utility.h>
- #include <proto/datatypes.h>
- #include <datatypes/datatypesclass.h>
- #include <datatypes/pictureclass.h>
- #include <datatypes/animationclass.h>
- #include <datatypes/datatypes.h>
- #include <errno.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <netinet/in.h>
- #include <proto/socket.h>
- #include <signal.h>
-
- // INFORMACOES DA VERSAO
- #define VERS "Freedom Internet Translator Client API v1.3"
- #define DATE "25.11.1999"
- #define VSTRING VERS " (" DATE ")"
- #define VERSTAG "\\0$VER: " VERS " (" DATE ")"
- const static char VersTag[] = VERSTAG;
-
- // NUMERO MAXIMO DE SOCKETS ABERTOS
- #define MAXSOCKS 10L
-
- // TAMANHO DO BUFFER DE LEITURA DE COMUNICACAO
- int TempBufferSize=65535;
-
- // PONTO DE ACESSO A UM HOST
- struct hostent *hptr;
- struct sockaddr_in server;
- int byteslidos,result,totalbytes;
- unsigned char *enderecoip;
- BPTR ArquivoCache=NULL;
- char *TempBuffer=NULL;
- char TempString[256], status[256];
- LONG TipoEndereco;
- char NomeDoHost[256];
- char direcao[50];
- int socket1;
- char opts[] = "ftudo";
-
- void top(char servidor[35])
- {
- printf("\nFreedom Internet Translator API v1.3 [25.11.1999]\n");
- printf("Server: %s\n", servidor);
- printf("By Cristian Robert Gallas (SuRRouND)\n");
- printf("Lithium Software 1999(r)\n");
- printf("FREEWARE WITH SOURCE FREE - CONTRIBUTE TO AMIGA OPEN SOURCE!\n\n");
- return;
- }
-
- void syntax(void)
- {
- printf("Syntax:\n");
- printf("-f[filename] Translate a File\n");
- printf("-t[text] Translate a Text\n");
- printf("-u[url] Translate a Url\n");
- printf("-d[x] Translate direction\n");
- printf("-o[filename] Output file to url translation (ram:url.html default)\n");
- printf("nofilters Disabled text filters\n");
- printf("noremove Dont remove the output file from ram, to debug!\n");
- printf("silence Dont show status messages\n");
- printf("silencemax Show only translated text\n");
- printf("server1 translator.go.com (default)\n");
- printf("server2 babelfish.altavista.digital.com\n");
- printf("help Show this help ;)\n\n");
- printf("Translate To: 0 - English to Francais\n");
- printf(" 1 - English to Deutsch\n");
- printf(" 2 - English to Italiano\n");
- printf(" 3 - English to Portugues\n");
- printf(" 4 - English to Espanol\n");
- printf(" 5 - Francais to English\n");
- printf(" 6 - Deutsch to English\n");
- printf(" 7 - Italiano to English\n");
- printf(" 8 - Espanol to English\n");
- printf(" 9 - Portugues to English\n\n");
- printf("Examples:\n");
- printf("FTrans -f \"ram:text.txt\" -d 3 silence\n");
- printf("FTrans -u \"http:\\www.microsoft.com\" -o \"ram:urltrans.html\" -d 3 nofilters\n");
- printf("FTrans -t \"Translate This!\" -d 2\n");
- printf("FTrans -f \"sys:amiga.txt\" -d 2 silencemax server2\n");
- printf("\n");
- return;
- }
-
- void main(int argc, char *argv[])
- {
- char *linha, *linhaf, *requisicao,
- opcao[10], lingua[10], tamanho[50], a,
- *saida, *teste, tmpfile[60], urlfile[256],
- arquivo[256], servidor[35], option,*odata;
- long int x = 0;
- int iservidor = 0, dir = 0, next, silence = 0,
- filter = 0, url = 0, remtmp = 0, max = 0;
- FILE *entrada;
-
-
- // SETA SERVER DEFAULT E URLFILE DEFAULT
- iservidor = 0;
- strcpy(servidor, "translator.go.com");
- strcpy(urlfile, "ram:url.html");
-
- // ACERTA O NOME DO ARQUIVO TEMPORARIO
- sprintf(tmpfile, "Ram:FreeTmp.%s", DataHora(7));
-
- // INICIA OS TESTES DOS COMANDOS COM ARGUMENTOS
- next = 1;
- while((odata = argopt(argc, argv, opts, &next, &option)) != NULL)
- {
- if (toupper(option) == 'F') {
- strcpy(opcao, "file");
- strcpy(arquivo, odata); }
-
- if (toupper(option) == 'T') {
- strcpy(opcao, "text");
- linhaf = malloc(strlen(odata));
- strcpy(linhaf, odata); }
-
- if (toupper(option) == 'U') {
- strcpy(opcao, "url");
- linhaf = malloc(strlen(odata));
- strcpy(linhaf, odata);
- // FAZ ALGUNS FILTROS NA URL
- strcpy(linhaf, TrimSur(linhaf, 0));
- strcpy(linhaf, SubstituiString(strlwr(linhaf), "http://", ""));
- strcpy(linhaf, SubstituiString(strlwr(linhaf), "http:", ""));
- url = 1; }
-
- if (toupper(option) == 'D') {
- dir = atoi(odata); }
-
- if (toupper(option) == 'O') {
- strcpy(urlfile, odata); }
- }
-
- // INICIA O TESTE COM OS ARGUMENTOS SOZINHOS
- for (; next < argc; next++)
- {
- // TESTA OPCAO SILENCE
- if(!strcmp(strupr(argv[next]), "SILENCE"))
- silence = 1;
- // TESTA OPCAO HELP
- if(!strcmp(strupr(argv[next]), "HELP")) {
- top(servidor);
- syntax();
- exit(0); }
- // TESTA OPCAO NOFILTERS
- if(!strcmp(strupr(argv[next]), "NOFILTERS"))
- filter = 1;
- // SETA SERVER 1 TRANSLATOR.GO
- if(!strcmp(strupr(argv[next]), "SERVER1")) {
- iservidor = 0;
- strcpy(servidor, "translator.go.com"); }
- // SETA SERVER 1 TRANSLATOR.GO
- if(!strcmp(strupr(argv[next]), "SERVER2")) {
- iservidor = 1;
- strcpy(servidor, "babelfish.altavista.digital.com"); }
- // PARAMETRO PARA NAO REMOVER O ARQUIVO TMP
- if(!strcmp(strupr(argv[next]), "NOREMOVE")) {
- remtmp = 1; }
- // PARAMETRO PARA MOSTRAR SOMENTE O TEXTO TRADUZIDO
- if(!strcmp(strupr(argv[next]), "SILENCEMAX")) {
- silence = 1;
- max = 1; }
- }
-
- // TESTA SE TEM INFORMACOES SUFICIENTES PARA PROCESSAR
- if(argc < 3) {
- top(servidor);
- syntax();
- exit(0); }
-
- // VERIFICA A LINGUA A TRADUZIR
- switch (dir)
- {
- case 0: strcpy(lingua, "en_fr");
- strcpy(direcao, "English to Francais"); break;
- case 1: if(iservidor == 0)
- strcpy(lingua, "en_ge");
- else
- strcpy(lingua, "en_de");
- strcpy(direcao, "English to Deutsch"); break;
- case 2: strcpy(lingua, "en_it");
- strcpy(direcao, "English to Italiano"); break;
- case 3: strcpy(lingua, "en_pt");
- strcpy(direcao, "English to Portugues"); break;
- case 4: if(iservidor == 0)
- strcpy(lingua, "en_sp");
- else
- strcpy(lingua, "en_es");
- strcpy(direcao, "English to Espanol"); break;
- case 5: strcpy(lingua, "fr_en");
- strcpy(direcao, "Francais to English"); break;
- case 6: if(iservidor == 0)
- strcpy(lingua, "ge_en");
- else
- strcpy(lingua, "de_en");
- strcpy(direcao, "Deutsch to English"); break;
- case 7: strcpy(lingua, "it_en");
- strcpy(direcao, "Italiano to English"); break;
- case 8: if(iservidor == 0)
- strcpy(lingua, "sp_en");
- else
- strcpy(lingua, "es_en");
- strcpy(direcao, "Espanol to English"); break;
- case 9: strcpy(lingua, "pt_en");
- strcpy(direcao, "Portugues to English"); break;
- default:strcpy(lingua, "en_pt");
- strcpy(direcao, "English to Portugues"); break;
- }
-
-
- // ROTINA QUE LE O CONTEUDO DE UM ARQUIVO TX
- if(!strcmp(strlwr(opcao), "file"))
- {
- // ROTINA QUE LE O ARQUIVO E JOGA NA VARIAVEL LINHA
- if(!(entrada = fopen(arquivo, "r")))
- {
- printf("%s: Access denied or file not found...\n\n", arquivo);
- goto SAIDAGOTO;
- }
- // ALOCA MEMORIA PARA AS VARIAVEIS DE PEDIDO
- x = atof(ExaminaArquivo(arquivo, 0));
- linhaf = malloc(x);
- linhaf[0] = '\0';
-
- // LE O ARQUIVO E JOGA NA VARIAVEL PARA TRADUCAO
- x = 0;
- while(a = getc(entrada),! feof(entrada))
- linhaf[x++] = a;
- linhaf[x] = '\0';
- fclose(entrada);
- }
-
- // ALOCA VARIAVEIS DE AUXILIO
- linha = malloc(strlen(linhaf) + 500);
- requisicao = malloc(strlen(linhaf) + 500);
- linha[0] = '\0'; requisicao[0] = '\0';
-
- // TITULO DO PROGRAMA
- if(silence == 0)
- top(servidor);
-
- // INICIA A CONECCAO NO SERVIDOR ABRINDO A SOCKET
- SockBase=(struct Library *)OpenLibrary("socket.library",0L);
- if(!(SockBase))
- {
- printf("You need connect on internet first...\n\n");
- goto SAIDAGOTO;
- }
- if(silence == 0)
- printf("Open Socket...\n");
-
- setup_sockets(MAXSOCKS,&errno);
-
- // PEGA INFORMACOES SOBRE O HOST
- if(!(hptr=gethostbyname(servidor)))
- {
- printf("Problem on translator server, try again later...\n\n");
- goto SAIDAGOTO;
- }
- memcpy((char *)&server.sin_addr,(char *)hptr->h_addr,hptr->h_length);
- // AF_INET = INTERNET
- server.sin_family=AF_INET;
-
- // PORTA 80 (SERVIDOR WEB)
- server.sin_port=80;
-
- TipoEndereco=hptr->h_addrtype;
- strcpy(NomeDoHost,hptr->h_name);
- enderecoip=(unsigned char *)hptr->h_addr_list[0];
- socket1=socket(hptr->h_addrtype, SOCK_STREAM,0);
-
- if(silence == 0)
- printf("Contacting host...\n");
-
- // CONECTA O SOCKET AO ENDERECO
- result=connect(socket1,(struct sockaddr*)&server,sizeof(server));
- if(result == -1)
- {
- printf("Connection problem, try again later...\n\n");
- goto SAIDAGOTO;
- }
- if(silence == 0)
- printf("Host contacted. Now Doing request...\n");
-
- // ROTINA QUE MONTA O PEDIDO DE TRADUCAO
- if(url == 0)
- if(iservidor == 0)
- sprintf(linha, "lp=%s&input_type=text&text=%s", lingua, linhaf);
- else
- sprintf(linha, "doit=done&urltext='%s'&languagepair=%s", linhaf, lingua);
- else
- if(iservidor == 0)
- sprintf(linha, "lp=%s&input_type=url&url=%s", lingua, linhaf);
- else
- sprintf(linha, "doit=done&urltext=http://%s&languagepair=%s", linhaf, lingua);
-
- sprintf(tamanho, "Content-Length: %d", strlen(linha));
-
- // INICIALIZA A REQUISICAO
- requisicao[0]='\0';
- if (iservidor == 0)
- strcpy(requisicao,"POST / HTTP/1.0");
- else
- strcpy(requisicao,"POST /cgi-bin/translate? HTTP/1.0");
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, "User-Agent: FreedomTranslator/1.2; (Amiga Translator Client)");
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, "Accept: */*;q=1");
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, "Host: ");
- strcat(requisicao, servidor);
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, tamanho);
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, "Content-Type: application/x-www-form-urlencoded");
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, linha);
- strcat(requisicao, "\x0d\x0a\0");
- strcat(requisicao, "\x0d\x0a\0");
-
- // ENVIA A REQUISICAO DE HTTP
- result=send(socket1,requisicao,strlen(requisicao),0);
- if(silence == 0)
- printf("Done http request. Waiting... ");
-
- totalbytes=0;
- if(!(ArquivoCache = Open(tmpfile, MODE_NEWFILE)))
- {
- printf("FTranslator port is open, binary FTranslator is in use, wait...\n\n");
- goto SAIDAGOTO;
- }
- TempBuffer=AllocMem(TempBufferSize,MEMF_FAST|MEMF_CLEAR);
- if(!(TempBuffer))
- {
- printf("Not have memory for allocate temporary buffer...\n");
- goto SAIDAGOTO;
- }
- do
- {
- byteslidos = 0;
- *TempBuffer = '\0';
- byteslidos = recv(socket1, TempBuffer, TempBufferSize-1,0);
- if(byteslidos)
- {
- Write(ArquivoCache, TempBuffer, byteslidos);
- totalbytes += byteslidos;
- TempString[0] = '\0';
- }
- }
- while(byteslidos>0);
-
- // FECHA O SOCKET E O ARQUIVO
- s_close(socket1);
- Close(ArquivoCache);
- FreeMem(TempBuffer,TempBufferSize);
- cleanup_sockets();
- CloseLibrary((struct Library *)SockBase);
-
- // ETAPA FINAL DO PROGRAMA
- if(silence == 0)
- printf("Done.\n");
-
- if(!(ExisteArquivo(tmpfile)))
- {
- printf("%s: Problem on temporary file...\n\n", tmpfile);
- goto SAIDAGOTO;
- }
-
- if(url == 0)
- {
- saida=malloc(atoi(ExaminaArquivo(tmpfile, 0)) + 500);
- teste=malloc(atoi(ExaminaArquivo(tmpfile, 0)) + 500);
-
- if(iservidor == 0)
- strcpy(saida, CortaStringArquivo(tmpfile, "<textarea name=\"transtext\" cols=\"52\" rows=\"20\" wrap=\"VIRTUAL\">", "</textarea>", 2));
- else
- strcpy(saida, CortaStringArquivo(tmpfile, "<font face=\"arial, helvetica\">", "<br><br></font></td></tr>", 2));
- } else {
- sprintf(saida, "Translate Url, Save on: %s\n", urlfile);
- }
-
- // FILTRA LINHA PARA MELHORAR A SAIDA DA TRADUCAO
- if(filter == 0 && url == 0)
- {
- if(silence == 0)
- printf("Applying filters... ");
-
- strcpy(teste, SubstituiString(saida, "<P>", "\n"));
- strcpy(saida, teste);
- strcpy(teste, SubstituiString(saida, "<", ""));
- strcpy(saida, teste);
- strcpy(teste, SubstituiString(saida, ">", ""));
- strcpy(saida, teste);
- strcpy(teste, SubstituiCaracter(saida, '`', ' '));
- strcpy(saida, teste);
- strcpy(teste, SubstituiCaracter(saida, '\'', ' '));
- strcpy(saida, teste);
- strcpy(teste, SubstituiCaracter(saida, '\"', '^'));
- strcpy(saida, teste);
- strcpy(teste, SubstituiCaracter(saida, '&', 'e'));
- strcpy(saida, teste);
- }
-
- if(silence == 0)
- printf("Done.\n\n");
-
- // MOSTRA A TRADUCAO
- if (max == 0)
- {
- printf(" Translation: %s\n", direcao);
- printf("o----------------------------------------------o\n");
- }
-
- // MENSAGEM TRADUZIDA
- printf("%s\n", saida);
-
- if (max == 0)
- {
- printf("o----------------------------------------------o\n");
- printf(" FTranslator API by Cristian Robert Gallas v1.3 \n");
- printf(" Freeware with Source Free (Amiga Open Source) \n");
- printf("o----------------------------------------------o\n");
- }
-
- // FINALIZA O PROGRAMA
- SAIDAGOTO:
- free(linhaf); free(linha); free(requisicao); free(saida);
- free(teste);
-
- if(url == 0)
- {
- if(remtmp == 0) {
- remove(tmpfile); }
- } else {
- remove(urlfile);
- rename(tmpfile, urlfile);
- }
-
- exit(0);
- }
-